﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using MarchEstate.Manager.Dao.Cash;

namespace MarchEstate.Manager.ObjectModel.Cash
{
    public class OsrccItem : AbstractOSRCCItemRelation
    {
        private CashManagerDao cashDao = null;

        public OsrccItem()
        {
        }

        public override void InitDao()
        {
            base.InitDao();
            cashDao = NewDao<CashManagerDao>();
        }

        
        public override List<AbstractOSRCCItemRelation> GetSrccItemList(string _srccId)
        {
            return cashDao.GetSrccItemList(_srccId);
        }


        public override bool Delete(string _srccId, string _itemcode)
        {
            if (_itemcode == "" || _itemcode == null)
                return cashDao.DeleteSrccItem(_srccId);
            else
                return cashDao.DeleteSrccItem(_srccId, _itemcode);
        }

        public override AbstractOSRCCItemRelation GetSrccItem(string _srccId, string _itemcode)
        {
            return cashDao.GetSrccItem(_srccId, _itemcode);
        }

        public override bool UpdateThisFee(string _srccId, string _itemcode, decimal _thisCostFee, decimal _thisLateFee, decimal _FavourFee, decimal _RealityFee, decimal _PrepayFee)
        {
            return cashDao.UpdateThisFee(_srccId, _itemcode, _thisCostFee, _thisLateFee, _FavourFee, _RealityFee, _PrepayFee);
        }

        public override bool UpdateThisFee(string _srccId, string _itemcode, DateTime _Lastmaturity, DateTime _Thismaturity, decimal _Lastsurplus, decimal _LastLateFee)
        {
            return cashDao.UpdateThisFee(_srccId, _itemcode, _Lastmaturity, _Thismaturity, _Lastsurplus, _LastLateFee);
        }

        public override bool UpdateThisFee(string _srccId, string _itemcode, DateTime _Thismaturity, decimal _thisCostFee, decimal _thisLateFee)
        {
            AbstractOSRCCItemRelation _srcc = GetSrccItem(_srccId, _itemcode);
            _srcc.ThisCostFee += _thisCostFee;
            _srcc.ThisLateFee += _thisLateFee;
            //实收金额(暂)=应收金额+滞纳金-上次余额-上次滞纳金-优惠金额
            //（如果 实收金额(暂)>=0）  实收金额=实收金额(暂) 、 本次余额=0
            //（如果 实收金额(暂)<0 ）  实收金额=0 、 本次余额=实收金额+优惠金额+上次余额+上次滞纳金-应收金额-滞纳金
            decimal _RealityFee = _srcc.ThisCostFee + _srcc.ThisLateFee - _srcc.Lastsurplus - _srcc.LastLateFee - _srcc.FavourFee;
            if (_RealityFee >= 0)
            {
                _srcc.RealityFee = _RealityFee;
                _srcc.PrepayFee = 0;
            }
            else
            {
                _srcc.RealityFee = 0;
                _srcc.PrepayFee = _srcc.RealityFee + _srcc.FavourFee + _srcc.Lastsurplus + _srcc.LastLateFee - _srcc.ThisCostFee - _srcc.ThisLateFee;
            }

            return cashDao.UpdateThisFee(_srccId, _itemcode,_Thismaturity, _srcc.ThisCostFee, _srcc.ThisLateFee, _srcc.FavourFee, _srcc.RealityFee, _srcc.PrepayFee);
        }

        public override bool UpdateThisFee(string _srccId, string _itemcode, DateTime _Lastmaturity, DateTime _Thismaturity, decimal _Lastsurplus, decimal _LastLateFee, decimal _thisCostFee, decimal _thisLateFee, decimal _FavourFee, decimal _RealityFee, decimal _PrepayFee)
        {
            return cashDao.UpdateThisFee(_srccId, _itemcode, _Lastmaturity, _Thismaturity, _Lastsurplus, _LastLateFee, _thisCostFee, _thisLateFee, _FavourFee, _RealityFee, _PrepayFee);
        }

        public override bool UpdateThisFee(string _srccId, string _itemcode, DateTime _Thismaturity, decimal _thisCostFee, decimal _thisLateFee, decimal _FavourFee, decimal _RealityFee, decimal _PrepayFee)
        {
            return cashDao.UpdateThisFee(_srccId, _itemcode,_Thismaturity, _thisCostFee, _thisLateFee, _FavourFee, _RealityFee, _PrepayFee);
        }
    }
}
